package userModule.controller;

import userModule.service.UserService;
import vo.Library_User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "RegisterServlet", value = "/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        UserService us = new UserService();

        //1.获取注册信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String major = request.getParameter("major");
        String phoneNum = request.getParameter("phoneNum");

        //2.根据注册信息封装一个User的实体类
        Library_User user = new Library_User(
                UUID.randomUUID().toString().toUpperCase(),
                username,
                password,
                role,
                major,
                phoneNum
        );


        //3.执行注册用户操作
        String backInfo = us.registerUser(user);

        //4.携带执行的结果转发到注册页面
        request.setAttribute("info",backInfo);

        request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);


    }
}
